﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace QuanLyShopCD.BL
{
    class cl_Danhmuc
    {
        AcessData con;
        Form frm;
        //String SQL;
        DataTable tb;

        public cl_Danhmuc(frm_QLDanhMuc2 f)
        {
            con = new AcessData();
            frm = f;          
        }
        public void Refesh()
        {
            ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;       
            ((TextBox)frm.Controls["txt_MaDM1"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_TenDM1"]).DataBindings.Clear();
    
            ((TextBox)frm.Controls["txt_MaDM1"]).DataBindings.Add("Text", tb, "MaDM");
            ((TextBox)frm.Controls["txt_TenDM1"]).DataBindings.Add("Text", tb, "TenDM");

        }

        public void Hienthi(string MaDM)
        {
            con.Open();
            //SqlCommand cmd = new SqlCommand();
            //cmd.Connection = con.GetConn;D:\New folder\QuanLyShopCD\QuanLyShopCD\Resources\
            //cmd.CommandText = "ps_LayDanhMuc";
            //cmd.CommandType = System.Data.CommandType.StoredProcedure;
            //SqlParameter p1;
            //if (MaDM != null)
            //    p1 = new SqlParameter("@MaDM", MaDM);
            //else
            //    p1 = new SqlParameter("@MaDM", DBNull.Value);
            //cmd.Parameters.Add(p1);
            //SqlDataAdapter adp = new SqlDataAdapter(cmd);
            //DataTable tb = new DataTable();
            //adp.Fill(tb);



            SqlParameter []pr = new SqlParameter[1];
            if (MaDM != null)
                pr[0] = new SqlParameter("@MaDM", MaDM);
            else
                pr[0] = new SqlParameter("@MaDM", DBNull.Value);
           tb = con.Execute_ps("ps_LayDanhMuc",pr);         
            Refesh();
            
        }

        public void them()
        {
            con.Open();
            //SqlCommand cmd = new SqlCommand();
            //cmd.Connection = con.GetConn;
            //cmd.CommandText = "ps_DanhMuc1";
            //cmd.CommandType = System.Data.CommandType.StoredProcedure;
            //SqlParameter p1 = new SqlParameter("@MaDM", MaDM);
            //SqlParameter p2 = new SqlParameter("@TenDM", TenDM);
            //cmd.Parameters.Add(p1);
            //cmd.Parameters.Add(p2);
            //SqlDataAdapter adp = new SqlDataAdapter(cmd);
            //DataTable tb = new DataTable();
            //adp.Fill(tb);
            //((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
            string str_MaDM, str_TenDM;
            str_MaDM = ((TextBox)frm.Controls["txt_madm1"]).Text;
            str_TenDM = ((TextBox)frm.Controls["txt_tendm1"]).Text;
            //SqlParameter[] pr = new SqlParameter[2];
            //pr[0] = new SqlParameter("@MaDM", str_MaDM);
            //pr[1] = new SqlParameter("@TenDM",str_TenDM);
            SqlParameter[] pr = { new SqlParameter("@MaDM", str_MaDM), new SqlParameter("@TenDM", str_TenDM) };
            tb = con.Execute_ps("ps_DanhMuc", pr);
            //((DataGridView)frm.Controls["dataGridView1"]).DataSource = tb;
            //((TextBox)frm.Controls["txt_MaDM1"]).DataBindings.Add("Text", tb, "MaDM");
            //((TextBox)frm.Controls["txt_TenDM1"]).DataBindings.Add("Text", tb, "TenDM");
            Refesh();
        }

        public void sua()
        {
            con.Open();
            //SqlCommand cmd = new SqlCommand();
            //cmd.Connection = con.GetConn;
            //cmd.CommandText = "ps_SuaDanhMuc";
            //cmd.CommandType = System.Data.CommandType.StoredProcedure;
            //SqlParameter p1 = new SqlParameter("@MaDM",MaDM);
            //SqlParameter p2 = new SqlParameter("@TenDM", TenDM);
            //cmd.Parameters.Add(p1);
            //cmd.Parameters.Add(p2);
            //SqlDataAdapter adp = new SqlDataAdapter(cmd);
            //DataTable tb = new DataTable();
            //adp.Fill(tb);
            //((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
            string str_MaDM, str_TenDM;
            str_MaDM = ((TextBox)frm.Controls["txt_madm1"]).Text;
            str_TenDM = ((TextBox)frm.Controls["txt_tendm1"]).Text;
            //SqlParameter[] pr = new SqlParameter[2];
            //pr[0] = new SqlParameter("@MaDM",MaDM);
            //pr[1] = new SqlParameter("@TenDM",TenDM);
            SqlParameter[] pr = { new SqlParameter("@MaDM",str_MaDM),new SqlParameter("@TenDM",str_TenDM)};

           tb = con.Execute_ps("ps_SuaDanhMuc",pr);
            ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
            //((TextBox)frm.Controls["txt_MaDM1"]).DataBindings.Add("Text", tb, "MaDM");
            //((TextBox)frm.Controls["txt_TenDM1"]).DataBindings.Add("Text", tb, "TenDM");
            Refesh();
        }

        public void xoa(string MaDM)
        {
            con.Open();
            //SqlCommand cmd = new SqlCommand();
            //cmd.Connection = con.GetConn;
            //cmd.CommandText = "ps_XoaDanhMuc";
            //cmd.CommandType = System.Data.CommandType.StoredProcedure;
            //SqlParameter p1 = new SqlParameter("@MaDM",MaDM);
            //cmd.Parameters.Add(p1);
            //SqlDataAdapter adp = new SqlDataAdapter(cmd);
            //DataTable tb = new DataTable();
            //adp.Fill(tb);
            //((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;

            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter("@MaDM",MaDM);
            tb = con.Execute_ps("ps_XoaDanhMuc", pr);
            ((DataGridView)frm.Controls["dataGridView1"]).DataSource = tb;
            Refesh();
        }
    }
}
